import * as echarts from '../../../ec-canvas/echarts';

var api = require('../../../utils/api.js');
var $ = require('../../../utils/util.js'); 
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp();
var Store = app.Store
Page({

  data: {

  },

 async onLoad (options) {
      console.log(options.info);
      this.setData({
        info:JSON.parse(options.info)
      })
      this.getData();
  },
  openMapHandle(){ // 打卡地图
    if(!this.data.info.ADDRESSPLACE){
      $.toast("对不起,该厂站未提供地址,无法开启地图服务")
    }else{
      wx.openLocation({
        latitude: this.data.info.LATITUDE,
        longitude: this.data.info.LONGITUDE,
        address: this.data.info.ADDRESSPLACE,
        scale: 16
      })
    }
  },
  init(data) {//电压 
    this.selectComponent('#mychart-dom-bar').init((canvas, width, height,dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr 
      });
      setOption(chart,data);
      this[chart] = chart;
      return chart;
    });
  },
  init1(data) {//电流
    this.selectComponent('#mychart-dom-bar1').init((canvas, width, height,dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr 
      });
      setOption1(chart,data);
      this[chart] = chart;
      return chart;
    });
  },
  async getData(){
    var params={
      name:this.data.info.name,
      addr:this.data.info.addr
    };
    wx.showLoading();
    var that=this;
    await api.$post(api.wave.info,params).then((res)=>{
        wx.hideLoading();
        that.setData({
          list_data: res.data,
        })
      })
      this.init(this.data.list_data);
      this.init1(this.data.list_data);
  }
})
function setOption(chart,result) { //电压
  const option = {
    color:['#c0c000', '#00c000', '#ff0000', '#96c864'],
    tooltip : {
        trigger: 'axis',
        axisPointer : {
          type : 'cross'
        }
    },
    legend: {
        data:['UA', 'UB', 'UC', 'UZ']
    },
    dataZoom: [
        {
            show: true,
            realtime: true,
            start: 0,
            end: 100
        },
        {
            type: 'inside',
            realtime: true,
            start: 0,
            end: 100
        }
    ],
    xAxis : [
        {
            type : 'category',
            boundaryGap : false,
            data: result.xList
        }
    ],
    yAxis: [
        {
            type: 'value'
        }
    ],
    series: [{
        name: 'UA',
        type: 'line',
        showSymbol: false,
        hoverAnimation: false,
        data: result.UA
    },{
        name: 'UB',
        type: 'line',
        showSymbol: false,
        hoverAnimation: false,
        data: result.UB
    },{
        name: 'UC',
        type: 'line',
        showSymbol: false,
        hoverAnimation: false,
        data: result.UC
    },{
        name: 'UZ',
        type: 'line',
        showSymbol: false,
        hoverAnimation: false,
        data: result.UZ
    }]
        }
  chart.setOption(option);
}
function setOption1(chart,result) { //电流
  const option = {
    color:['#c0c000', '#00c000', '#ff0000', '#96c864'],
    tooltip : {
        trigger: 'axis',
        axisPointer : {
          type : 'cross'
        }
    },
    legend: {
        data:['IA', 'IB', 'IC', 'IZ']
    },
    dataZoom: [
        {
            show: true,
            realtime: true,
            start: 0,
            end: 100
        },
        {
            type: 'inside',
            realtime: true,
            start: 0,
            end: 100
        }
    ],
    xAxis : [
        {
            type : 'category',
            boundaryGap : false,
            data: result.xList
        }
    ],
    yAxis: [
        {
            type: 'value'
        }
    ],
    series: [{
        name: 'IA',
        type: 'line',
        showSymbol: false,
        hoverAnimation: false,
        data: result.IA
    },{
        name: 'IB',
        type: 'line',
        showSymbol: false,
        hoverAnimation: false,
        data: result.IB
    },{
        name: 'IC',
        type: 'line',
        showSymbol: false,
        hoverAnimation: false,
        data: result.IC
    },{
        name: 'IZ',
        type: 'line',
        showSymbol: false,
        hoverAnimation: false,
        data: result.IZ
    }]
  }
  chart.setOption(option);
}